// pages/xiaofeibi/xiaofeibi.js
const app=getApp();
//获得全局js变量
var util = require('../../utils/util.js');
Page({
  data: {
    loadingType: 0,
    contentText: {
      contentdown: "上拉显示更多",
      contentrefresh: "正在加载...",
      contentnomore: "没有更多数据了"
    },
    typelist: [],
    good: [],
    p: 1,
    id: 0,
    typeid: 0,
    keyword:''
  },

  onLoad(options) {
    this.setData({
      id: options.id,
      typeid: options.typeid
    });
    this.setData({ p: 1 });
    if (this.data.typeid <= 0) {
      this.getGoodCate();
    }
    this.getInfo();
  },
  onInput(e) {
      this.setData({
          keyword: e.detail.value
      });
  },
  search() {
        const key = this.data.keyword;
        if (!key) {
            wx.showToast({
                title: '请输入关键字',
                icon: 'none',
                duration: 1500
            });
            return;
        }
        this.getInfo();
    },
  getGoodCate() {
    const that = this;
    util.httpRrquest(app.data.apiurl + 'mallshop/getGoodCate?id='+that.data.id, 'GET')
    .then(function (response) {
            var data = response.data.data;
        that.setData({
          typelist: data
        });
    }, function (error) {
        console.log(error);
    });
  },

  getInfo() {
    const that = this;
    util.httpRrquest(app.data.apiurl + 'mallshop/getGoodList?id='+that.data.id+'&page='+that.data.page+'&cateId='+that.data.typeid+'&keyword='+that.data.keyword, 'GET')
    .then(function (response) {
            var data = response.data.data;
        that.setData({
          good: data
        });
    }, function (error) {
        console.log(error);
    });
  },

  jumpTo(event) {
    const id = event.currentTarget.dataset.id;
    var that=this;
    wx.navigateTo({
      url: '/pages/mallShopGood/mallShopGood?typeid='+id+'&id='+that.data.id
    });
  },

  jumpToDetail(event) {
    const id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail?id='+id+'&from=sp'
    });
  }
});